package view.card
{
	import flash.display.Bitmap;
	
	import global.GameType;
	import global.Resource;
	
	import model.card.ICardData;
	
	import utils.ColorTool;
	import utils.DisplayTool;

	public class Card extends AbsCard
	{
		private var bg:Bitmap;
		private var valueImg:Bitmap;
		private var colorImg:Bitmap;
		private var jokerValueImg:Bitmap;
		
		public function Card()
		{
			initImgs();
		}
		
		private function initImgs():void
		{
			bg = Resource.getImg("card_bg");
			addChild(bg);
			
			colorImg = new Bitmap();
			addChild(colorImg);
			valueImg = new Bitmap();
			addChild(valueImg);
			jokerValueImg = new Bitmap();
			addChild(jokerValueImg);
		}
		
		override public function updateData(cardData:ICardData) : void
		{
			super.updateData(cardData);
			setValueImg(cardData.value);
			setColorImg(cardData.type);
			setJokerValue(cardData.jokerValue);
		}
		
		private function setJokerValue(v:int):void
		{
			if (v != -1)
			{
				
				valueImg.bitmapData = Resource.getBitmapData("card_" + v);
				if (data.type == GameType.CARD_IMAGE_JOKER_BLACK)
				{
					jokerValueImg.bitmapData = Resource.getBitmapData("card_18")
			
				}
				else if (data.type == GameType.CARD_IMAGE_JOKER_RED)
				{
					jokerValueImg.bitmapData = Resource.getBitmapData("card_19")
				}
			}
			jokerValueImg.y = 13;
			jokerValueImg.x = 2;
		}
		
		private function setValueImg(v:int):void
		{
			if (v == GameType.CARD_VALUE_JOKER)
			{
				if (data.type == GameType.CARD_IMAGE_JOKER_BLACK)
				{
					valueImg.bitmapData = Resource.getBitmapData("card_" + v);
				}
				else if (data.type == GameType.CARD_IMAGE_JOKER_RED)
				{
					valueImg.bitmapData = Resource.getBitmapData("card_" + (v + 1));
				}
			}
			else
			{
				valueImg.bitmapData = Resource.getBitmapData("card_" + v);
			}
			valueImg.x = this.width - valueImg.width >> 1;
			valueImg.y = this.height - valueImg.height >> 1;
		}
		
		private function setColorImg(v:int):void
		{
			var imgKey:String = "";
			switch (v)
			{
				case GameType.CARD_IMAGE_CUBS:
					imgKey = "card_cubs";
					ColorTool.black(valueImg);
					break;
				case GameType.CARD_IMAGE_DIAMONS:
					imgKey = "card_diamons";
					break;
				case GameType.CARD_IMAGE_HEARTS:
					imgKey = "card_hearts";
					break;
				case GameType.CARD_IMAGE_SPADES:
					imgKey = "card_spades";
					ColorTool.black(valueImg);
					break;
				case GameType.CARD_IMAGE_JOKER_BLACK:
					imgKey = "card_jokerB";
					break;
				case GameType.CARD_IMAGE_JOKER_RED:
					imgKey = "card_jokerR";
					break;
			}
			colorImg.bitmapData = Resource.getBitmapData(imgKey);
			colorImg.x = this.width - colorImg.width >> 1;
			colorImg.y = this.height - colorImg.height >> 1;
		}
		
		override public function destroy():void
		{
			super.destroy();
			bg.bitmapData = null;
			bg = null;
			valueImg.bitmapData = null;
			valueImg = null;
			colorImg.bitmapData = null;
			colorImg = null;
			jokerValueImg.bitmapData = null;
			jokerValueImg = null;
			DisplayTool.removeAllChild(this);
		}
	}
}